package cn.kxx.utils;


import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

/**
 * 阿里云发送短信验证码
 */
public class SMSUtils {

    public static void sendShortMessage(String templateCode,String phoneNum,String params){
        try {
            Config config = new Config();
            config.setAccessKeyId("LTAI5tGoHPDePFZYvtUwXcHY");
            config.setAccessKeySecret("8tXqx4Rgna6AiQDRCODrqDdNxzxop7");
            Client client = new Client(config);
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setSignName("阿里云短信测试") //短信签名
                    //.setTemplateCode(templateCode)
                    .setTemplateCode("SMS_154950909") //短信的模板code
                    //.setPhoneNumbers(phoneNum)
                    .setPhoneNumbers("19503817257") //接收验证码的手机号
                    .setTemplateParam("{\"code\":"+params+"}"); //验证码
            RuntimeOptions runtime = new RuntimeOptions();
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            //打印
            String code = sendSmsResponse.getBody().getCode();
            String message = sendSmsResponse.getBody().getMessage();
            System.out.println("调用阿里云短信服务接口，响应的code="+code+",message="+message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
